//
//  DataProvider.m
//  LoveAttention
//
//  Created by 鞠超 on 16/1/4.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "DataProvider.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFURLRequestSerialization.h"
#import "SVProgressHUD.h"
#define Url @"http://115.28.67.86:8080/aigz/"


@implementation DataProvider

#pragma mark 赋值回调
- (void)setDelegateObject:(id)cbobject setBackFunctionName:(NSString *)selectorName
{
    CallBackObject = cbobject;
    
    callBackFunctionName = selectorName;
}

-(void)GetRequest:(NSString *)url andpram:(NSDictionary *)pram
{
    AFHTTPRequestOperationManager * manage=[[AFHTTPRequestOperationManager alloc] init];
    manage.responseSerializer=[AFHTTPResponseSerializer serializer];
    manage.requestSerializer=[AFHTTPRequestSerializer serializer];
    manage.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/plain"];//可接收到的数据类型
    manage.requestSerializer.timeoutInterval=10;//设置请求时限
    NSDictionary * prm =[[NSDictionary alloc] init];
    if (pram!=nil) {
        prm=pram;
    }
    [manage GET:url parameters:prm success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}


#pragma mark - 上传用户资料
-(void)PostRequest:(NSString *)url andpram:(NSDictionary *)pram
{
    AFHTTPRequestOperationManager * manage=[[AFHTTPRequestOperationManager alloc] init];
    manage.responseSerializer=[AFHTTPResponseSerializer serializer];
    manage.requestSerializer=[AFHTTPRequestSerializer serializer];
    manage.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];//可接收到的数据类型
    manage.requestSerializer.timeoutInterval=10;//设置请求时限
    NSDictionary * prm =[[NSDictionary alloc] init];
    if (pram!=nil) {
        prm=pram;
    }
    [manage POST:url parameters:prm success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        NSDictionary * dict =responseObject;
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}
- (void)uploadImageWithImage:(NSString *)imagePath andurl:(NSString *)url andprm:(NSDictionary *)prm andkey:(NSString *)key
{
    NSData *data=[NSData dataWithContentsOfFile:imagePath];
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:prm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:data name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpg"];
    }];
    
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
        NSLog(@"上传完成");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"上传失败->%@", error);
        [SVProgressHUD dismiss];
    }];
    
    //执行
    NSOperationQueue * queue =[[NSOperationQueue alloc] init];
    [queue addOperation:op];
    //    FileDetail *file = [FileDetail fileWithName:@"avatar.jpg" data:data];
    //    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
    //                            file,@"FILES",
    //                            @"avatar",@"name",
    //                            key, @"key", nil];
    //    NSDictionary *result = [HttpRequest upload:[NSString stringWithFormat:@"%@index.php?act=member_index&op=avatar_upload",Url] widthParams:params];
    //    NSLog(@"%@",result);
}

- (void)ShowOrderuploadImageWithImage:(NSData *)imagedata andurl:(NSString *)url andprm:(NSDictionary *)prm andkey:(NSString *)key
{
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:prm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imagedata name:@"files" fileName:@"files.png" mimeType:@"image/png"];
    }];
    
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
        NSLog(@"上传完成");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"上传失败->%@", error);
        [SVProgressHUD dismiss];
    }];
    
    //执行
    NSOperationQueue * queue =[[NSOperationQueue alloc] init];
    [queue addOperation:op];
    //    FileDetail *file = [FileDetail fileWithName:@"avatar.jpg" data:data];
    //    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
    //                            file,@"FILES",
    //                            @"avatar",@"name",
    //                            key, @"key", nil];
    //    NSDictionary *result = [HttpRequest upload:[NSString stringWithFormat:@"%@index.php?act=member_index&op=avatar_upload",Url] widthParams:params];
    //    NSLog(@"%@",result);
}


#pragma mark - 登陆
-(void)Login_inwith:(NSString *)username andpwd:(NSString *)pwd
{
    if (username&&pwd) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mlogin",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}",username,pwd]};
        [self GetRequest:url andpram:prm];
    }
}
#pragma mark - 注册
-(void)Registerwith:(NSString *)username andpwd:(NSString *)pwd andcode:(NSString *)code andinvitecode:(NSString *)invitecode
{
    if (username&&pwd&&code&&invitecode) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mRegister",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"mobile\":\"%@\",\"password\":\"%@\",\"code\":\"%@\",\"invitecode\":\"%@\",\"client\":\"0\"}",username,pwd,code,invitecode]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 我的积分
- (void)myJifenWith:(NSString *)merchantid
{
    if (merchantid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mIntegral",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\"}",merchantid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 输入验证码
- (void)inputCaptchaWith:(NSString *)code
{
    if (code) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/identifycode",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"code\":\"%@\"}",code]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 关注用户
- (void)AttentionWith:(NSString *)merchantid andpagenumber:(NSString *)pagenumber andpagesize:(NSString *)pagesize
{
    if (merchantid && pagenumber && pagesize) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/attentionUsers",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"pagenumber\":\"%@\",\"pagesize\":\"%@\"}",merchantid,pagenumber,pagesize]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 用户资料
- (void)UserDetailWith:(NSString *)memberid
{
    if (memberid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/memberdetail",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"memberid\":\"%@\"}",memberid]};
        [self GetRequest:url andpram:prm];
    }
}


#pragma mark - 上传头像
-(void)Image_headwith:(NSString *)merchantid andfiles:(NSData *)files
{
    if (merchantid && files) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/uploadAvatar",Url];
//        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"files\":\"%@\"}",merchantid,files]};
        NSDictionary * prm=@{@"merchantid":merchantid};
//        [self uploadImageWithImage:files andurl:url andprm:prm andkey:nil];
        [self ShowOrderuploadImageWithImage:files andurl:url andprm:prm andkey:nil];
    }
}

#pragma mark - 预约我的产品
- (void)ReservationWith:(NSString *)merchantid andpagenumber:(NSString *)pagenumber andpagesize:(NSString *)pagesize
{
    if (merchantid && pagenumber && pagesize) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/reserationUsers",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"pagenumber\":\"%@\",\"pagesize\":\"%@\"}",merchantid,pagenumber,pagesize]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 预约详情
- (void)ReservationDetailWith:(NSString *)reserationid
{
    if (reserationid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/reserationdetail",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"reserationid\":\"%@\"}",reserationid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 修改预约状态
-(void)reviseReservationwith:(NSString *)reserationid andrstate:(NSString *)rstate
{
    if (reserationid && rstate) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/reserationUpdate",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"reserationid\":\"%@\",\"rstate\":\"%@\"}",reserationid,rstate]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 预约删除
- (void)ReservationDeleteWith:(NSString *)reserationid
{
    if (reserationid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/reserationDel",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"reserationid\":\"%@\"}",reserationid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 卡券核销
- (void)CardDallocWith:(NSString *)merchantid andpagenumber:(NSString *)pagenumber andpagesize:(NSString *)pagesize
{
    if (merchantid && pagenumber && pagesize) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/verificationindex",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"pagenumber\":\"%@\",\"pagesize\":\"%@\"}",merchantid,pagenumber,pagesize]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 商户积分变化记录
- (void)jifenChangeWith:(NSString *)merchantid andpagenumber:(NSString *)pagenumber andpagesize:(NSString *)pagesize
{
    if (merchantid && pagenumber && pagesize) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/integralList",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"pagenumber\":\"%@\",\"pagesize\":\"%@\"}",merchantid,pagenumber,pagesize]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 卡券数据
- (void)CardDataWith:(NSString *)merchantid
{
    if (merchantid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/coupondata",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\"}",merchantid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 商户确认订单
- (void)ConfirmOrdersWith:(NSString *)orderid
{
    if (orderid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mconfirmorder",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"orderid\":\"%@\"}",orderid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 支付
- (void)RechargeWith:(NSString *)merchantid andchannel:(NSString *)channel andamount:(NSString *)amount
{
    if (merchantid && channel && amount) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/merchantIntegralAdd",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"channel\":\"%@\",\"amount\":\"%@\"}",merchantid,channel,amount]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 我的邀请
- (void)inviteWithMemberid:(NSString *)memberid
{
    if (memberid) {
        NSString * url=[NSString stringWithFormat:@"%@data/invite",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"memberid\":\"%@\"}",memberid]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 卡券核销
- (void)couponConsumeWithMerchantid:(NSString *)merchantid couponnum:(NSString *)couponnum
{
    if (merchantid && couponnum)
    {
        NSString * url=[NSString stringWithFormat:@"%@mdata/couponConsume",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"couponnum\":\"%@\"}",merchantid,couponnum]};
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - .卡券搜索
- (void)couponSearchWithMerchantid:(NSString *)merchantid couponnum:(NSString *)couponnum
{
    if (merchantid && couponnum)
    {
        NSString * url=[NSString stringWithFormat:@"%@mdata/couponSearch",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"couponnum\":\"%@\"}",merchantid,couponnum]};
        [self GetRequest:url andpram:prm];
    }
}


#pragma mark - 卖家版卡券详情
- (void)coupondetailWithCouponid:(NSString *)couponid
{
    if (couponid) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/coupondetail",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"couponid\":\"%@\"}",couponid]};
        [self GetRequest:url andpram:prm];
    }
}


#pragma mark - 卡券核销
- (void)couponConsumeWithMerchantid:(NSString *)merchantid couponid:(NSString *)couponid
{
    if (merchantid && couponid)
    {
        NSString * url=[NSString stringWithFormat:@"%@mdata/couponConsume",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"couponid\":\"%@\"}",merchantid,couponid]};
        [self GetRequest:url andpram:prm];
    }
}


#pragma mark - 买家订单列表
- (void)orderlistWith:(NSString *)merchantid andpagenumber:(NSString *)pagenumber andpagesize:(NSString *)pagesize andordernum:(NSString *)ordernum
{
    if (merchantid && pagenumber && pagesize && ordernum) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/orderlist",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"merchantid\":\"%@\",\"pagenumber\":\"%@\",\"pagesize\":\"%@\",\"ordernum\":\"%@\"}",merchantid,pagenumber,pagesize,ordernum]};
        [self GetRequest:url andpram:prm];
    }
}


#pragma mark - 我的订单详情
- (void)orderdetailWithOrderid:(NSString *)orderid
{
    if (orderid) {
        NSString * url=[NSString stringWithFormat:@"%@data/orderdetail",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"orderid\":\"%@\"}",orderid]};
        
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 12.用户操作订单
- (void)orderdetailWithOrderid:(NSString *)orderid opera:(NSString *)opera memberid:(NSString *)memberid
{
    if (orderid && opera && memberid)
    {
        NSString * url=[NSString stringWithFormat:@"%@data/orderState",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"orderid\":\"%@\",\"opera\":\"%@\",\"memberid\":\"%@\"}",orderid,opera,memberid]};
        
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 3.商户操作订单
- (void)mconfirmorderWithOrderid:(NSString *)orderid opera:(NSString *)opera merchanid:(NSString *)merchanid
{
    if (orderid && opera && merchanid)
    {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mconfirmorder",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"orderid\":\"%@\",\"opera\":\"%@\",\"merchanid\":\"%@\"}",orderid,opera,merchanid]};
        
        [self GetRequest:url andpram:prm];
    }
}

#pragma mark - 5.根据确认码确认消费
- (void)identifycodeWithcode:(NSString *)code merchantid:(NSString *)merchantid
{
    if (code && merchantid)
    {
        NSString * url=[NSString stringWithFormat:@"%@mdata/identifycode",Url];
        NSDictionary * prm=@{@"json":[NSString stringWithFormat:@"{\"code\":\"%@\",\"merchantid\":\"%@\"}",code,merchantid]};
        
        [self GetRequest:url andpram:prm];
    }
}


-(void)GetHangye
{
    NSString * url=[NSString stringWithFormat:@"%@mdata/getDiscount",Url];
    
    [self GetRequest:url andpram:nil];
}

-(void)UploadShopInfoWithprm:(NSDictionary * )prm andavatar:(NSData *)avatar andcardz:(NSData *)cardz andcardf:(NSData *)cardf andyyzz:(NSData *)yyzz
{
    if (prm) {
        NSString * url=[NSString stringWithFormat:@"%@mdata/mUpLoadMerchantInfo",Url];
        
        NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:prm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:avatar name:@"avatar" fileName:@"avatar.png" mimeType:@"image/png"];
            [formData appendPartWithFileData:cardz name:@"cardz" fileName:@"cardz.png" mimeType:@"image/png"];
            [formData appendPartWithFileData:cardf name:@"cardf" fileName:@"cardf.png" mimeType:@"image/png"];
            [formData appendPartWithFileData:yyzz name:@"yyzz" fileName:@"yyzz.png" mimeType:@"image/png"];
        }];
        
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
            id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            SEL func_selector = NSSelectorFromString(callBackFunctionName);
            if ([CallBackObject respondsToSelector:func_selector]) {
                NSLog(@"回调成功...");
                [CallBackObject performSelector:func_selector withObject:dict];
            }else{
                NSLog(@"回调失败...");
                [SVProgressHUD dismiss];
            }
            NSLog(@"上传完成");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"上传失败->%@", error);
            [SVProgressHUD dismiss];
        }];
        
        //执行
        NSOperationQueue * queue =[[NSOperationQueue alloc] init];
        [queue addOperation:op];
        //    FileDetail *file = [FileDetail fileWithName:@"avatar.jpg" data:data];
        //    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
        //                            file,@"FILES",
        //                            @"avatar",@"name",
        //                            key, @"key", nil];
        //    NSDictionary *result = [HttpRequest upload:[NSString stringWithFormat:@"%@index.php?act=member_index&op=avatar_upload",Url] widthParams:params];
        //    NSLog(@"%@",result);
    }
}











@end
